<template>
<div>
    <el-form ref="tform"  :model='dataForm' label-width="80px">
        <el-form-item label="仓库名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="请输入仓库名称"></el-input>
        </el-form-item>
        <el-form-item label="位置" prop="location">
            <el-input v-model="dataForm.location" type="textarea" placeholder="请输入位置" style="width:70%;"></el-input>
        </el-form-item>
        <el-form-item label="添加标签" prop="tags">
            <el-input v-model="dataForm.tags" type="textarea" placeholder="请输入备注" style="width:70%;"></el-input>
            <p style="color:#999;font-size:12px;">多个标签，用','分隔</p>
        </el-form-item>
        <el-form-item label="管理人">
<el-select v-model="dataForm.staff_id" filterable placeholder="请选择管理人">
            <el-option
              v-for="item in staffs"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>  
                  </el-form-item>
    </el-form>

</div>
</template>

<script>
import headTop from "@/components/headTop";

import {
    insertStore,
    updateStore,
    getStaffList
} from "@/api/getData";
export default {
    inject: ["reload"],
    data() {
        return {
            isEdit: false,
            staffs:[]
        };
    },
    created() {
      getStaffList().then((res)=>{
          this.staffs = res.data.list;
      });
    },
    props: ['dataForm'],
    components: {

    },
    methods: {
        onInsert() {
            this.isEdit = false;
            this.vilateForm();
        },
        onUpdate() {
            this.isEdit = true;
            this.vilateForm();
        },
        vilateForm() {
            this.$refs['tform'].validate(async valid => {
                if (valid) {
                    if (this.isEdit) {
                        this.updateStores();
                    } else {
                        this.insertStores();
                    }
                } else {
                    this.$notify.error({
                        title: "错误",
                        message: "请检查输入是否正确",
                        offset: 100
                    });
                    return false;
                }
            });
        },
        resetFields() {
            this.$refs['tform'].resetFields();
        },
        async insertStores() {
            try {
                let params = this.dataForm;
                console.log(params);
                const result = await insertStore(params);
                if (result.code == 200) {
                    this.$message("添加成功");
                    this.$emit('insertSuccess')
                } else {
                    throw new Error("获取数据失败");
                }
            } catch (err) {
                console.log("获取数据失败", err);
            }
        },
        async updateStores() {
            let params = this.dataForm;
            updateStore(params).then((res) => {
                    if (res.code == 200) {
                        this.$message("修改成功");
                        this.$emit('insertSuccess')
                    }
                })
                .catch((err) => {

                });
        },
        changeSwitch(data) {
            console.log(data);
        }
    }
};
</script>

<style lang="less" scoped>

.el-form .el-input {
    width: 70%;
}
.el-form {
    margin: 10px;
}
</style>
